package com.kedou.sdk.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import androidx.fragment.app.FragmentActivity;

import com.kedou.sdk.params.bean.SimpleUserInfo;
import com.kedou.sdk.ui.assist.AccountTokenListAdapter;
import com.kedou.sdk.ui.assist.OnFinishActivityListener;

/**
 * Created by Administrator on 2018/3/8.
 */
public class AccountListTokenDialogFragment extends BaseDialogFragment implements View.OnClickListener, AdapterView.OnItemClickListener {


    private ImageButton mReturnLoginBtn;
    private ListView mAccountListView;
    private TextView accountText;
    private TextView accountAddText;
    private TextView swichLayout;

//    private View lastSelectedView;
//    private int lastSelectedPosition;

    private AccountTokenListAdapter mListAdapter;
    SimpleUserInfo info;

    public static final AccountListTokenDialogFragment newInstance(Bundle bundle) {
        AccountListTokenDialogFragment fragment = new AccountListTokenDialogFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        View contentView = doInflateContentView(inflater, container, getLayoutIdentifier("hnssn_activity_accounttoken_lists"));


        mAccountListView = contentView.findViewById(getViewIdentifier("activity_account_user_account_lv"));
        mReturnLoginBtn = contentView.findViewById(getViewIdentifier("activity_login_finish_btn"));
        accountText = contentView.findViewById(getViewIdentifier("activity_account_main"));
        swichLayout = contentView.findViewById(getViewIdentifier("accountlist_swichAccount"));
        accountAddText = contentView.findViewById(getViewIdentifier("accountlist_addaccount"));
        accountAddText.setOnClickListener(this);
        mReturnLoginBtn.setOnClickListener(this);
        mAccountListView.setOnItemClickListener(this);
        swichLayout.setOnClickListener(this);
        mListAdapter = new AccountTokenListAdapter();

        Bundle bundle = getArguments();
        if (bundle != null) {
             info= bundle.getParcelable("user");
            accountText.setText(info.getUsername());
            mListAdapter.setAdapterData(bundle.<SimpleUserInfo>getParcelableArrayList("userInfos"));
        }

        mAccountListView.setAdapter(mListAdapter);
        return contentView;
    }

    @Override
    public void onClick(View view) {
        if (mReturnLoginBtn == view||swichLayout ==view) {
//            AccountListDialogFragment.OnUserLoginActionListener listener = getOnUserLoginActionListener();
//            if (listener != null) {
//                SimpleUserInfo userInfo = mListAdapter.getItem(lastSelectedPosition);
//                listener.doSubmitLoginAccountInfo(userInfo, null);
//            }
            OnCreateXiaohaoActionListener listener = getOnUserLoginActionListener();
            if (listener != null) {
                listener.doFinishActivity(this);
            }
        }else if (view==accountAddText){
            OnCreateXiaohaoActionListener listener = getOnUserLoginActionListener();
            if (listener != null) {
                listener.doCreateXiaohao(this);
            }
        }
    }

    public interface OnCreateXiaohaoActionListener extends OnFinishActivityListener {

        void doCreateXiaohao(BaseDialogFragment fragment);
    }


    private OnCreateXiaohaoActionListener getOnUserLoginActionListener() {
        if (!isNullContext()) {
            FragmentActivity activity = getActivity();
            if (activity instanceof OnCreateXiaohaoActionListener) {
                return (OnCreateXiaohaoActionListener) activity;
            }
        }
        return null;
    }

    private OnUserLoginActionListener getLoginListener() {
        if (!isNullContext()) {
            FragmentActivity activity = getActivity();
            if (activity instanceof OnUserLoginActionListener) {
                return (OnUserLoginActionListener) activity;
            }
        }
        return null;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Log.e("login","onItemClick");
        OnUserLoginActionListener listener = getLoginListener();
            if (listener != null) {
                SimpleUserInfo userInfo = mListAdapter.getItem(position);
                listener.doSubmitAuthLoginXiaohaoInfo(userInfo);
            }
    }
    public interface OnUserLoginActionListener extends OnFinishActivityListener {

        void doSubmitAuthLoginXiaohaoInfo(SimpleUserInfo userInfo);
    }

}
